/**
 * Licensed under the Apache License Version 2.0.
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package net.interknit.knitter.fw;

import java.util.Enumeration;
import java.util.Properties;

public class CustomProperties
{
	public static final String CUSTOM_TYPE_FACTORY_PROP_FILE_NAME =
			"custom-type-factory.properties";
	public static final String TYPE_INSTANTIATOR_PROPERTY_NAME =
			"custom.type.instantiator";
	public static final String IO_ADAPTER_PROPERTY_NAME =
			"custom.io.adapter";
	public static final String ACCESS_CONTROLLER_NAME =
			"custom.access.controller";
	public static final String SERVER_INITIALIZER_NAME =
			"custom.server.initializer";

	private static CustomProperties instance;

	public static CustomProperties getInstance()
	{
		return instance != null ? instance : (instance = new CustomProperties());
	}

	private final Properties props;

	private CustomProperties()
	{
		props = new Properties();

		try
		{
			props.load(getClass().getClassLoader().getResourceAsStream(
					CUSTOM_TYPE_FACTORY_PROP_FILE_NAME));

			System.out.println("Interknit: " + CUSTOM_TYPE_FACTORY_PROP_FILE_NAME +
					" loaded from classpath, which defines the following properties:");

			for(Enumeration<?> en = props.propertyNames(); en.hasMoreElements(); )
			{
				String key = (String) en.nextElement();
				String val = props.getProperty(key);

				System.out.println("\t" + key + "=" + val);

				// set the property to System property for future use
				// System.setProperty(key, val);
			}
		}
		catch(Exception e)
		{
			System.err.println("Interknit: " + CUSTOM_TYPE_FACTORY_PROP_FILE_NAME + " " +
					"not found on classpath. Will use default properties.");
		}
	}

	public static String getCustomProperty(String key)
	{
		return getInstance().props.getProperty(key, "");
	}
}
